use std::fs;
use std::io::Write;
use std::path::Path;
use tauri::command;

#[command]
pub fn read_file(path: String) -> Result<String, String> {
    fs::read_to_string(&path).map_err(|e| e.to_string())
}

#[command]
pub fn write_file(path: String, content: String) -> Result<(), String> {
    fs::File::create(&path)
        .and_then(|mut file| file.write_all(content.as_bytes()))
        .map_err(|e| e.to_string())
}

/// 确保目录存在，如果不存在则创建
#[command]
pub fn ensure_directory(dir_path: String) -> Result<(), String> {
    fs::create_dir_all(&dir_path).map_err(|e| e.to_string())
}

/// 检查文件或目录是否存在
#[command]
pub fn path_exists(path: String) -> Result<bool, String> {
    Ok(Path::new(&path).exists())
}

/// 保存配置文件到custom-config目录
#[command]
pub fn save_config_file(filename: String, content: String) -> Result<String, String> {
    // 确保custom-config目录存在
    let config_dir = "./custom-config";
    fs::create_dir_all(config_dir).map_err(|e| format!("创建custom-config目录失败: {}", e))?;

    // 构建完整的文件路径
    let file_path = format!("{}/{}", config_dir, filename);

    // 写入文件
    fs::File::create(&file_path)
        .and_then(|mut file| file.write_all(content.as_bytes()))
        .map_err(|e| format!("保存配置文件失败: {}", e))?;

    Ok(file_path)
}

/// 从custom-config目录读取配置文件
#[command]
pub fn read_config_file(filename: String) -> Result<String, String> {
    let file_path = format!("./custom-config/{}", filename);
    fs::read_to_string(&file_path).map_err(|e| format!("读取配置文件失败: {}", e))
}

/// 保存缓存文件到cache目录
#[command]
pub fn write_cache_file(filename: String, content: String) -> Result<String, String> {
    // 确保cache目录存在
    let cache_dir = "./cache";
    fs::create_dir_all(cache_dir).map_err(|e| format!("创建cache目录失败: {}", e))?;

    // 构建完整的文件路径
    let file_path = format!("{}/{}", cache_dir, filename);

    // 写入文件
    fs::File::create(&file_path)
        .and_then(|mut file| file.write_all(content.as_bytes()))
        .map_err(|e| format!("保存缓存文件失败: {}", e))?;

    Ok(file_path)
}

/// 从cache目录读取缓存文件
#[command]
pub fn read_cache_file(filename: String) -> Result<String, String> {
    let file_path = format!("./cache/{}", filename);
    match fs::read_to_string(&file_path) {
        Ok(content) => Ok(content),
        Err(_) => Ok(String::new()) // 文件不存在时返回空字符串而不是错误
    }
}

/// 清除缓存文件
#[command]
pub fn clear_cache_file(filename: String) -> Result<(), String> {
    let file_path = format!("./cache/{}", filename);
    if Path::new(&file_path).exists() {
        fs::remove_file(&file_path).map_err(|e| format!("删除缓存文件失败: {}", e))?;
    }
    Ok(())
}

/// 获取缓存文件信息
#[command]
pub fn get_cache_info(filename: String) -> Result<serde_json::Value, String> {
    let file_path = format!("./cache/{}", filename);
    
    if !Path::new(&file_path).exists() {
        return Ok(serde_json::json!({
            "exists": false
        }));
    }

    match fs::read_to_string(&file_path) {
        Ok(content) => Ok(serde_json::json!({
            "exists": true,
            "content": content
        })),
        Err(e) => Err(format!("读取缓存文件失败: {}", e))
    }
}
